;(function($){
	$.fn.selection_fromProfileCompany = function(opt){
		var el = this;
		var opts = {
			condition:{ho_profile_where:{search:""}},//获取数据源条件json
			contextPath:getContextPath(),
			data:{
				code : "gname", //需要获取字段名
				container : "company", //填入input的ID
				multiSelect : "no", //是否多选
			},
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opts,opt);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.initGrid();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//档案类型
//			el.find("input[name=ptype]").click(function(){
//				el.find("#btnFilterSearch_ProfileCompany").trigger("click");
//			});
			
			//清空
			el.find("#modalBtnClear").click(function(){
				el.find("#filterSearch_ProfileCompany").val("");
				el.find("#btnFilterSearch_ProfileCompany").trigger("click");
			});
			
			//确定按钮
			el.find("#selectionModalSave_ProfileCompany").click(function(){
				if(param.data.multiSelect=="yes"){
					var ids=el.find("#modalGrid").jqGrid('getGridParam','selarrrow');
					if(ids.length>0){
						param.dataBack(ids.join(","));
					}else{
						param.dataBack("");
					}
				}else{
					param.dataBack(param.rowdata);
				}
				el.modal("hide");
				el.empty();
			})
			
			//清空按钮按钮
			el.find("#selectionModalDel_ProfileCompany").click(function(){
				if(param.data.multiSelect=="yes"){
					param.dataBack("");
				}else{
					var data = {pno:0}
					param.dataBack(data)
				}
				el.modal("hide");
				el.empty();
			})
			
			//查询框回车事件
			el.find("#filterSearch_ProfileCompany").keydown(function(e){
				if(e.keyCode == 13){
					el.find("#btnFilterSearch_ProfileCompany").trigger("click");
				}
			})
			
			//查询按钮查询
			el.find("#btnFilterSearch_ProfileCompany").click(function(){
				t.freGridValue();
			})
			
			//取消退出
			el.find("#selectionModalCansel_ProfileCompany,#close-dialog").click(function(){
				param.closeBack();
				el.modal("hide");
				el.empty();
			})
		},
		initHtml : function(){
			this.el.addClass("modal").attr("tabindex","-1").attr("data-backdrop","static");
			html='<div class="modal-dialog" style="min-width: 1000px;">'+
	    			'<div class="modal-content">'+
			  			'<div class="modal-header no-padding">'+
							'<div class="table-header">'+
								'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
									'<span class="white">&times;</span>'+
								'</button>'+
								'<span id="modal-h4">'+$.i18n.propJsp("客人档案选择")+'</span>'+ 
							'</div>'+
						'</div>'+
			  			'<div class="modal-body">'+
							'<div class="row">'+
								'<div class="col-xs-8">'+
									'<div class="well well-sm">'+
						  				'<span class="clearfix">'+
							  				'<span id="searchFilter" class="pull-left">'+
						  						'<input class="input-xlarge" id="filterSearch_ProfileCompany" value="'+this.param.condition.ho_profile_where.search+'" type="text" placeholder="'+$.i18n.propJsp("姓名")+'/'+$.i18n.propJsp("证件号")+'/'+$.i18n.propJsp("电话")+'/'+$.i18n.propJsp("别名")+'"/>&nbsp;&nbsp;'+
						  						'<span class="btn-group" style="margin-top:-5px;">'+
													'<button id="modalBtnClear" class="btn btn-sm btn-info">'+
														'<i class="ace-icon fa fa-trash"></i>'+
														$.i18n.propJsp("清空")+
													'</button>'+
												'</span>'+
						  						'<span class="btn-group" style="margin-top:-5px;">'+
													'<button id="btnFilterSearch_ProfileCompany" class="btn btn-sm btn-info">'+
														'<i class="ace-icon fa fa-search"></i>'+
														$.i18n.propJsp("查询")+
													'</button>'+
												'</span>'+
											'</span>'+
//											'<span class="pull-right">'+
//												'<label>'+
//													'<input name="ptype" value="2" type="radio" class="ace" checked/>'+
//													'<span class="lbl">'+$.i18n.propJsp("公司")+'</span>'+
//												'</label>&nbsp;&nbsp;'+
//												'<label>'+
//													'<input name="ptype" value="3" type="radio" class="ace"/>'+
//													'<span class="lbl">'+$.i18n.propJsp("旅行社")+'</span>'+
//												'</label>&nbsp;&nbsp;'+
//												'<label>'+
//													'<input name="ptype" value="4" type="radio" class="ace"/>'+
//													'<span class="lbl">'+$.i18n.propJsp("其他形式")+'</span>'+
//												'</label>'+
//											'</span>'+
										'</span>'+
									'</div>'+
									'<div class="widget-box">'+
						  				'<div id="gridWidth" class="widget-body">'+
											'<table id="modalGrid"></table>'+
											'<div id="modalPage"></div>'+
										'</div>'+
									'</div>'+
								'</div>'+
								'<div class="col-xs-4">'+
									'<div class="widget-box">'+
										'<div class="widget-body">'+
											'<div class="widget-main" style="height:400px;">'+
												'<div id="profileInfo" class="sidebar-info">'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">PNO</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="pno"></span>'+
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("公司名")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="gname"></span>'+
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("营销员")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="sales_name"></span>'+
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("费率连接")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="ratelink"></span>'+
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">VIP</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="vip"></span>'+
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("客户级别")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="levels"></span>'+
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("电话")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="tel"></span>'+
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("联系人")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="cname"></span>'+
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("地址")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="addr"></span>'+
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("上次房号")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="last_room"></span>'+
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("上次联系日期")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="last_conc"></span>'+
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("备注")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="remark"></span>'+
														'</div>'+
													'</div>'+
												'</div>'+
											'</div>'+
											'<div class="widget-toolbox padding-8 clearfix" style="height:53px;">'+
												'<div class="pull-right" style="margin-top:4px;">'+
													'<div class="btn-group">'+
														'<button class="btn btn-info btn-sm" id="selectionModalDel_ProfileCompany" data-dismiss="modal">'+
															'<i class="ace-icon fa fa-trash"></i>'+$.i18n.propJsp("清空公司")+
														'</button>'+
													'</div>'+
													'<div class="btn-group">'+
														'<button class="btn btn-info btn-sm" id="selectionModalSave_ProfileCompany">'+
															'<i class="ace-icon fa fa-check"></i>'+$.i18n.propJsp("确定")+
														'</button>'+
													'</div>'+
													'<div class="btn-group">'+
														'<button id="selectionModalCansel_ProfileCompany" class="btn btn-default btn-sm" data-dismiss="modal">'+
															'<i class="ace-icon fa fa-times"></i>'+$.i18n.propJsp("取消")+
														'</button>'+
													'</div>'+
												'</div>'+
											'</div>'+
										'</div>'+
									'</div>'+		
								'</div>'+
							'</div>'+
						'</div>'+
	    			'</div>'+
	    		'</div>';
			this.el.html(html);
			this.el.modal("show");
		},
		initGrid : function(){
			
			var el = this.el;
			var param = this.param;
			var grid_selector = this.el.find("#modalGrid");
			var pager_selector = this.el.find("#modalPage");
			this.getDs(function(initc){
                grid_selector.jqGrid({
                    url : $.HSAPI_AJAXURL + $.HSAPI_PROCEDUREGRID,
                    datatype : "json",
                    width :643,
                    height : 300,
                    postData : $.HSAPI_ASKJSONINFO( JSON.stringify(initc),'RetrieveJqGridList',"list"),
                    colNames : ["PNO","公司名","别名","营销员","费率连接","联系人","地址",
                                "城市","国家","VIP","客户级别","电话","上次房号",
                                "上次日期","备注","来源","市场"],
                    colModel : [{
                        name : "pno",//"PNO",
                        index : "pno",
                        width : 80,
                        key : true,
                        hidden : true,
                    },{
                        name : "gname",//"公司名", 
                        index : "gname",
                    },{
                        name : "fname",//"别名",
                        index : "fname",
                        width : 150,
                        hidden : true,
                    },{
                        name : "sales_name",//"营销员",
                        index : "sales_name",
                        width: 100,
                    },{
                        name : "ratelink",//"费率连接",
                        index : "ratelink",
                        width: 100,
                    },{
                        name : "cname",//"联系人",
                        index : "cname",
                        width: 100,
                    },{
                        name : "addr",//"地址",
                        index : "addr",
                        width: 240,
                    },{
                        name : "city",//"城市",
                        index : "city",
                        width : 150,
                        hidden : true,
                    },{
                        name : "country",//"国家",
                        index : "country",
                        width : 150,
                        hidden : true,
                    },{
                        name : "vip",//"VIP",
                        index : "vip",
                        width : 150,
                        hidden : true,
                    },{
                        name : "levels",//"客户级别",
                        index : "levels",
                        width : 150,
                        hidden : true,
                    },{
                        name : "tel",//"电话",
                        index : "tel",
                        width : 150,
                        hidden : true,
                    },{
                        name : "last_room",//"上次房号",
                        index : "last_room",
                        width : 150,
                        hidden : true,
                    },{
                        name : "last_conc",//"上次日期",
                        index : "last_conc",
                        width : 150,
                        hidden : true,
                    },{
                        name : "remark",//"备注",
                        index : "remark",
                        width : 150,
                        hidden : true,
                    },{
                        name : "sou",//"来源",
                        index : "sou",
                        width : 150,
                        hidden : true,
                    },{
                        name : "group_",//"市场",
                        index : "group_",
                        width : 150,
                        hidden : true,
                    }],   	
                    scroll : true,
                    sortname : "pno",
                    sortorder : "asc", 
                    viewrecords : true,
                    rowNum : "20",
                    rowList:["全部",10,20,30],
                    emptyrecords: "没有搜索到任何数据",
                    recordtext: "共计:{2}条",
                    multiselect : (param.data.multiSelect=="yes"?true:false),
                    pager : pager_selector,
                    altRows : true, 
                    scrollrows:false, 
                    ondblClickRow: function(rowid){ 
                        param.rowdata = grid_selector.jqGrid("getRowData",rowid);
                        el.find("#selectionModalSave_ProfileCompany").trigger("click")
                    },
                    onSelectRow:function(rowid){
                        param.rowdata = grid_selector.jqGrid("getRowData",rowid);
                        el.find("#profileInfo span").each(function(){
                            $(this).text(param.rowdata[this.id])
                            $(this).attr("title",param.rowdata[this.id]);
                        })
                    },
                    loadComplete : function(data){
                        var table = this;
                        setTimeout(function(){
                            updatePagerIcons(table);
                            enableTooltips(table);
                        }, 0);
                        var ids = jQuery(grid_selector).jqGrid('getDataIDs');
                        grid_selector.jqGrid('setSelection',ids[0]);
                    }, 
                    gridComplete:function(){
                        jqgridColResize();
                    }
                });
            });
		},
		freGridValue : function(){
			var param = this.param;
			var grid_selector = this.el.find("#modalGrid");
			grid_selector.jqGrid("GridUnload");
			this.initGrid();
		},
		getDs : function(back){
		
			var el = this.el;
			var parm = this.param;
			var wherestring = {};
			this.getSQLCondition(function(str){
                wherestring = {wherestring:["@@WS", str ]};
                var where = {};
                where.where = {"1":"1"};
                var fileds = {};
                fileds = {"fileds":["pno","gname","fname",
                                    "sales_name","ratelink","addr",
                                    "city","country","vip",
                                    "levels","tel","last_room",
                                    "last_conc","remark","sou","group_","cname"
                                    ]};
            
                var table = {};
                table.table={"table":"viewJqGrid_rsv_profile_all_company"};				
                var c = jQuery.extend({}, where,fileds,table,wherestring);
                back(c)
            });

		},
		//刷新数据源
		getSQLCondition : function(back){
			var el = this.el;
			var param = this.param;
			var json = {ho_profile_where:{search:el.find("#filterSearch_ProfileCompany").val().trim()}};
            var str = " 1=1 ";
            $.HSAPI_CALLPROCEDURE2(json,"HoWSASCSelectHoProfileCompany",{},function(data){
                str = data.ho_profileSQL;
                if(data.RETURN == 1){
                    str = data.ho_profileSQL;
                }else{
                    $.HSAPI_ALERT(data.MSG);
                }
                back(str);
            },function(data){
                back(str)
                $.HSAPI_ALERT(JSON.stringify(data));
            })
		}
	}
	
	/*
	 * 私有函数
	 * 翻页栏
	 * 
	 */
	function updatePagerIcons(table) {
		var replacement = {
				"ui-icon-seek-first" : "ace-icon fa fa-angle-double-left bigger-140",
				"ui-icon-seek-prev" : "ace-icon fa fa-angle-left bigger-140",
				"ui-icon-seek-next" : "ace-icon fa fa-angle-right bigger-140",
				"ui-icon-seek-end" : "ace-icon fa fa-angle-double-right bigger-140"
		};
		$(".ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon").each(function(){
			var icon = $(this);
			var $class = $.trim(icon.attr("class").replace("ui-icon", ""));

			if ($class in replacement)
				icon.attr("class", "ui-icon " + replacement[$class]);
		})
	};
	
	/*
	 * 私有函数
	 * 提示框
	 * 
	 */
	function enableTooltips(table) {
		$('.navtable .ui-pg-button').tooltip({
			container : 'body'
		});
		$(table).find('.ui-pg-div').tooltip({
			container : 'body'
		});
	};	
	/*
	 * 私有函数
	 * 获取站点路径
	 */
	function getContextPath(){
		var pathName = document.location.pathname;
		var index = pathName.substr(1).indexOf("/");
		var path = pathName.substr(0, index + 1);
		return path;
	}
	
})(jQuery);
var maxWidth="150";
function jqgridColResize(){
    var td=$("#modalGrid").find('td[aria-describedby="modalGrid_gname"]')//获取计算实际列长度的容器  
        ,tds//临时保存列  
        ,arr=[];//用于保存最大的列宽  
     //遍历每行获得每行中的最大列宽  
    $(td).each(function(){    
         maxWidth=Math.max(maxWidth,getWidthOfText($(this).text()));                 
    }); 
    
    $(td).each(function(){ 
    	$('#modalGrid_gname').each(function(idx){this.style.width=maxWidth+10+'px'}); 
    	$("#modalGrid").find(".jqgfirstrow").find("td")[1].style.width=maxWidth+10+'px';    
   });
    
  }

function getWidthOfText(txt, fontname="Open Sans", fontsize="13px"){ 
    if(getWidthOfText.c === undefined){
        getWidthOfText.c=document.createElement('canvas');
        getWidthOfText.ctx=getWidthOfText.c.getContext('2d');
    }
    getWidthOfText.ctx.font = fontsize + ' ' + fontname;
    return getWidthOfText.ctx.measureText(txt).width;
}